
package com.wing.member.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.LoginMember;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.member.model.response.MemberLoginLogVO;
import com.wing.member.model.entity.Member;
import com.wing.member.service.MemberLoginLogService;
import com.wing.web.utils.LocaleMessage;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * @author
 */
@Api(value = "App-会员登录记录管理", tags = "会员登录记录管理")
@RestController("appMemberLoginLogController")
@RequestMapping("member/app/memberLoginLog")
public class MemberLoginLogController {

    @Autowired
    private MemberLoginLogService memberLoginLogService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping()
    public PageResult<MemberLoginLogVO> list(
        @LoginMember Member member
        ,Integer pageNum
        ,Integer pageSize
        ,String sort
        ,String order
    ){
        Page<MemberLoginLogVO> page = new Page<>(pageNum, pageSize);
        Map<String, Object> params = new HashMap<>();
    
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        params.put("memberId", member.getId());

        Page<MemberLoginLogVO> list = memberLoginLogService.pageList4app(page, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@LoginMember Member member, @PathVariable("id") Long memberLoginLogId) {
        MemberLoginLogVO memberLoginLogVO = memberLoginLogService.get4appById(memberLoginLogId);
        if(memberLoginLogVO == null){
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), memberLoginLogVO);
    }

}
